package indi.kane.simpleproject.service.impl;

import indi.kane.simpleproject.dao.MemberMapper;
import indi.kane.simpleproject.entity.Member;
import indi.kane.simpleproject.entity.Profession;
import indi.kane.simpleproject.service.IMemberService;
import indi.kane.simpleproject.util.RedisUtil;
import indi.kane.simpleproject.vo.MemberVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: MemberService
 * @Author: kane
 * @Date: 2019-11-05 5:35 下午
 * @Description:
 */
@Service
public class MemberService implements IMemberService {
    @Resource
    private MemberMapper memberMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ProfessionService professionService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Member record) {
        return memberMapper.insert(record);
    }

    @Override
    public int insertSelective(Member record) {
        return memberMapper.insertSelective(record);
    }

    @Override
    public MemberVo selectByPrimaryKey(Integer id) {
        MemberVo memberVo = new MemberVo();
        Member member = memberMapper.selectByPrimaryKey(id);
        List<Profession> professions = (List<Profession>) redisUtil.lGetIndex("professions",-1);
        if(professions == null){
            professions = professionService.loadAll();
        }
        Profession profession =  professions.stream().filter(item -> item.getId().equals(member.getProfession())).findAny().get();
        memberVo.setId(member.getId());
        memberVo.setName(member.getName());
        memberVo.setAge(member.getAge());
        memberVo.setGender(member.getGender());
        memberVo.setProfession(member.getProfession());
        memberVo.setUpdatedBy(member.getUpdatedBy());
        memberVo.setUpdatedOn(member.getUpdatedOn());
        memberVo.setProfessionVo(profession);
        return memberVo;
    }

    @Override
    public int updateByPrimaryKeySelective(Member record) {
        return memberMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return memberMapper.updateByPrimaryKey(record);
    }
}